<?php

class Con_users extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->load->library('session');

        $this->load->database();
        $this->load->model('model_users');
    }

    // start new edit
    function index() {
        $data['mytitle'] = "Mainpage";
        $data['data'] = $this->model_users->select_login();
        $this->load->view('view_mainpage', $data);
    }

    // edit new edit
    // start new edit
    function login() {
        $data['mytitle'] = "Mainpage";
        if ((isset($_POST['username']) || isset($_POST['password'])) == "") {
            $this->load->view('view_mainpage', $data);
        } else {
            $data['sql_name'] = $this->model_users->select_name();
            $data['data'] = $this->model_users->select_login();
            $this->load->view('view_mainpage_menu', $data);
        }
    }

    // end new edit
    // start new edit
    function logout() {
        $this->session->sess_destroy();
        redirect('con_users');
    }

    // end new edit
    // start new edit
    function registers() {
        $data['mytitle'] = "Registers";

        if (count($_POST) > 0) { // input type post > true
            $myname = $_POST['myname'];
            $mysurname = $_POST['mysurname'];
            $username = $_POST['username'];
            $password = $_POST['password'];
            $email = $_POST['email'];

            // check data not null
            if ($myname != "" && $mysurname != "" && $username != "" && $password != "" && $email != "") {
                $this->model_users->insert_registers();
                $data['data'] = $this->model_users->select_login();
                $this->load->view('view_registers_success', $data);
            } else { // data = null
                $data['data'] = $this->model_users->select_login();
                $this->load->view('view_registers_error', $data);
            }
        } else { // input type post > false
            $data['data'] = $this->model_users->select_login();
            $this->load->view('view_registers', $data);
        }
    }

    // end new edit
//------------------------------------------------------------------------------
// start personmanager

    function active_person() {
        $data['mytitle'] = "Person Manager";

        if (isset($_POST['select_type']) != null) {
            $data['data'] = $this->model_users->select_login();
            $data['data1'] = $this->model_users->select_person_type();
            $this->load->view('view_active_person', $data);
        } else {
            if (isset($_POST['update_status_person_id']) != null) {
                $this->model_users->update_active_users();
            }

            if (isset($_POST['delete_person']) != null) {
                $this->model_users->delete_active_users();
            }

            $data['data'] = $this->model_users->select_login();
            $data['data1'] = $this->model_users->select_person();
            $this->load->view('view_active_person', $data);
        }
    }

    function Newperson() {
        $data['mytitle'] = "New Person";
        $data['qry_group'] = $this->model_users->select_group();
        $data['qry_personid'] = $this->model_users->select_personid();
        $data['data'] = $this->model_users->select_login();
        $this->load->view('View_newperson', $data);
    }

    function Addperson() {
        $this->model_users->insert();

        $data['data'] = $this->model_users->select_login();
        $data['data1'] = $this->model_users->select_person();
        $this->load->view('view_active_person', $data);
    }

    function Editpersonpage() {
        $data['mytitle'] = "Edit Person";
        if (isset($_POST['editperson']) != null) {
            $data['data'] = $this->model_users->select_login();
            $data['data1'] = $this->model_users->select_edit();
            $data['qry_group'] = $this->model_users->select_group();
            $data['qry_chkgroup'] = $this->model_users->select_chkgroup();
            $this->load->view('view_editperson', $data);
        }
    }

    function Editperson() {
        $data['show'] = $this->model_users->edit_users();

        $data['data'] = $this->model_users->select_login();
        $data['data1'] = $this->model_users->select_person();
        $this->load->view('view_active_person', $data);
    }

//------------------------------------------------------------------------------
// start groupmanager

    function Group_person() {
        $data['mytitle'] = "Group-Person Manager";
        if (isset($_POST['select_group_id']) != null) {
            $data['data'] = $this->model_users->select_login();
            $data['data1'] = $this->model_users->select_gperson_type();
            $this->load->view('view_groupmanager', $data);
        } else {
            if (isset($_POST['activegperson']) != null) {
                $data['show'] = $this->model_users->update_active_groupperson();
            }
            if (isset($_POST['deletegperson']) != null) {
                $data['show'] = $this->model_users->delete_groupperson();
            }

            $data['data'] = $this->model_users->select_login();
            $data['data1'] = $this->model_users->select_group();
            $this->load->view('view_groupmanager', $data);
        }
    }

    function Newgperson() {
        $data['mytitle'] = "New Group-Person";
        $data['data'] = $this->model_users->select_login();
        $this->load->view('View_newgroup', $data);
    }

    function Addgperson() {
        $this->model_users->insertgroup();
        
        $data['data'] = $this->model_users->select_login();
        $data['data'] = $this->model_users->select_group();
        $this->load->view('view_groupmanager', $data);
    }

    function Editgpersonpage() {
        $data['mytitle'] = "Edit Group-Person";
        if (isset($_POST['editgperson']) != null) {
            $data['data'] = $this->model_users->select_login();
            $data['data1'] = $this->model_users->select_editgroup();
            $this->load->view('view_editgroup', $data);
        }
    }

    function Editgperson() {
        $data['show'] = $this->model_users->edit_groupusers();
        
        $data['data'] = $this->model_users->select_login();
        $data['data1'] = $this->model_users->select_group();
        $this->load->view('view_groupmanager', $data);
    }

//------------------------------------------------------------------------------
// start GROUP CONTROL

    function group_control() {
        $data['mytitle'] = "Group-Person & Group-menu Control";
        if (isset($_POST['deletecontrolgroup']) != null) {
            $data['testing'] = $this->model_users->delete_controlgroup();
        }
        
        $data['data'] = $this->model_users->select_login();
        $data['gperson'] = $this->model_users->select_group();
        $data['gmenu'] = $this->model_users->select_control_group();
        $this->load->view('view_controlgroup', $data);
    }

    function Editcontrolpage() {
        $data['mytitle'] = "Edit Group-Person & Group-menu Control";
        if (isset($_POST['editgperson']) != null) {
            $data['data'] = $this->model_users->select_login();
            $data['data1'] = $this->model_users->select_editcontrolgroup();
            $data['qry_group'] = $this->model_users->select_gmenu();
            $data['qry_chkgroup'] = $this->model_users->select_chkcontrolgroup();
            $this->load->view('view_editcontrol', $data);
        }
    }

    function Editcontrol() {
        $data['data'] = $this->model_users->select_login();
        $data['show'] = $this->model_users->edit_controlgroup();
        $this->group_control();
    }

//------------------------------------------------------------------------------
// start menumanager

    function Menu() {
        $data['mytitle'] = "MENU";
        if (isset($_POST['select_menu_all_id']) != null) {
            $data['menumanag'] = $this->model_users->select_menu_all_type();
            $data['data'] = $this->model_users->select_login();
            $this->load->view('view_menumanager', $data);
        } else {
            if (isset($_POST['activemenu']) != null) {
                $data['testing'] = $this->model_users->active_menu();
            }
            if (isset($_POST['deletemenu']) != null) {
                $data['testing'] = $this->model_users->delete_menu();
            }
            
            $data['data'] = $this->model_users->select_login();
            $data['menumanag'] = $this->model_users->select_menu();
            // get groupmenu in listbox
            $data['gmenu'] = $this->model_users->select_gmenu();
            $this->load->view('view_menumanager', $data);
        }
    }

    function show_menu() {
        $data['mytitle'] = "Menu";
        $data['data'] = $this->model_users->select_login();
        $data['menumanag'] = $this->model_users->select_menu_of_group();
        $data['gmenu'] = $this->model_users->select_gmenu();
        $this->load->view('view_menumanager', $data);
    }

    function new_menu() {
        $data['mytitle'] = "New Menu";
        $data['data'] = $this->model_users->select_login();
        $data['group'] = $this->model_users->select_gmenu();
        $data['menuid'] = $this->model_users->select_menuid_max();
        $this->load->view('view_newmenu', $data);
    }

    function new_menu2() {
        $data['mytitle'] = "New Menu";
        $this->model_users->new_menu();
        $this->menu();
    }

    function edit_menu() {
        $data['mytitle'] = "Edit Menu";
        $data['data'] = $this->model_users->select_login();
        $data['edit'] = $this->model_users->edit_menu();
        $data['group'] = $this->model_users->select_gmenu();
        $data['chkgroup'] = $this->model_users->chk_group_menu();
        $this->load->view('view_editmenu', $data);
    }

    function edit_menu2() {
        $data['mytitle'] = "Edit Menu";
        $this->model_users->edit_menu2();
        $this->menu();
    }

//------------------------------------------------------------------------------
// start GROUPmenumanager  

    function group_menu() {
        $data['mytitle'] = "Group Menu";
        if (isset($_POST['group_menu_type']) != null) {
            $data['gmenu'] = $this->model_users->select_gmenu_type();
            $data['data'] = $this->model_users->select_login();
            $this->load->view('view_groupmenu', $data);
        } else {
            if (isset($_POST['activegmenu']) != null) {
                $data['testing'] = $this->model_users->active_gmenu();
            }
            if (isset($_POST['deletegmenu']) != null) {
                $data['testing'] = $this->model_users->delete_gmenu();
            }
            $data['data'] = $this->model_users->select_login();
            $data['gmenu'] = $this->model_users->select_gmenu();
            $this->load->view('view_groupmenu', $data);
        }
    }

    function new_gmenu() {
        $data['mytitle'] = "New Group Menu";
        $data['data'] = $this->model_users->select_login();
        $this->load->view('view_newgroupmenu', $data);
    }

    function new_gmenu2() {
        $this->model_users->new_gmenu();
        $this->group_menu();
    }

    function edit_gmenu() {
        $data['mytitle'] = "Edit Group Menu";
        $data['data'] = $this->model_users->select_login();
        $data['edit'] = $this->model_users->edit_gmenu();
        $this->load->view('view_editgroupmenu', $data);
    }

    function edit_gmenu2() {
        $data['mytitle'] = "Edit Group Menu";
        $this->model_users->edit_gmenu2();
        $this->group_menu();
    }

}

?>
